import axios from 'axios'

const state = {
    rectifySource: [],
    inspectSource: [],

}

const mutations = {
    SET_RECTIFY_SOURCE: (state, data) => {
        state.rectifySource = data;
    },
    SET_INSPECT_SOURCE: (state, data) => {
        state.inspectSource = data;
    }
}

const actions = {
    loadSource(context) {
        context.dispatch("loadInspectSource");
        context.dispatch("loadRectifySource");
    },
    loadInspectSource({ commit, rootGetters }) {
        let minDate =
            new Date(new Date().getTime() - 365 * 24 * 3600000)
            .toLocaleDateString()
            .replace(/\//g, "-") + " 00:00:00";
        let maxDate =
            new Date().toLocaleDateString().replace(/\//g, "-") + " 23:59:59";
        let filter = [{
                keys: ["begin"],
                operator: "<=",
                values: [
                    [maxDate]
                ],
            },
            {
                keys: ["time"],
                operator: ">=",
                values: [
                    [minDate]
                ],
            },
        ];
        // console.log(filter);
        axios
            .post("api/query_inspect_record/", {
                user: rootGetters.userbase,
                filter,
            })
            .then((response) => {
                if (response.data.status === 0) {
                    commit("SET_INSPECT_SOURCE", response.data.content);
                } else {
                    console.log(response);
                }
            })
            .catch((exception) => {
                console.log(exception);
            });
    },
    loadRectifySource({ commit, rootGetters }) {
        let startDate = new Date();
        let endDate = new Date(startDate.getTime() - 24 * 3600000 * 999);
        let startDateString =
            startDate.getFullYear() +
            "-" +
            (startDate.getMonth() + 1) +
            "-" +
            startDate.getDate() +
            " 23:59:59";
        let endDateString =
            endDate.getFullYear() +
            "-" +
            (endDate.getMonth() + 1) +
            "-" +
            endDate.getDate() +
            " 00:00:00";
        let userbase = rootGetters.userbase;
        console.log(userbase);
        axios
            .post("api/query_rectify_record_short/", {
                user: userbase,
                filter: {
                    startTime: endDateString,
                    endTime: startDateString,
                },
            })
            .then((response) => {
                console.log(response.data);
                if (response.data.status === 0) {
                    commit("SET_RECTIFY_SOURCE", response.data.content);
                } else {
                    console.log("query_rectify_record_short:\n");
                    console.log(response.data);
                }
            })
            .catch((exception) => {
                console.log("query_rectify_record_short:\n");
                console.log(exception);
            });
        // inspect
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}